<template>
  <div class="building-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <div class="search-label">楼宇名称：</div>
      <el-input v-model="params.name" placeholder="请输入内容" class="search-main" @clear="NameCha" @change="ChongZhi" />
      <el-button type="primary" @click="NameCha">查询</el-button>
    </div>
    <div>
      <el-dialog
        :title="addFrom.id?'编辑楼宇': '添加楼宇' "
        :visible="dialogVisible"
        width="580px"
        @close="closeDialog"
      >
        <template #footer>
          <!-- 表单接口 -->
          <div class="form-container">
            <el-form ref="FromBiaoDan" :model="addFrom" :rules="addFromList">
              <el-form-item label="楼宇名称" prop="name">
                <el-input v-model="addFrom.name" />
              </el-form-item>
              <el-form-item label="楼宇层数" prop="floors">
                <el-input v-model="addFrom.floors" />
              </el-form-item>
              <el-form-item label="在管面积" prop="area">
                <el-input v-model="addFrom.area" />
              </el-form-item>
              <el-form-item label="物业费" prop="propertyFeePrice">
                <el-input v-model="addFrom.propertyFeePrice" />
              </el-form-item>
            </el-form>
          </div>

          <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
          <el-button size="mini" type="primary" @click="confirmAdd">确 定</el-button>
        </template>
      </el-dialog>

      <el-button type="primary" @click="addBuilding">添加楼宇</el-button>

    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table
        style="width: 100%"
        :data="list"
      >
        <el-table-column
          type="index"
          label="序号"
          :index="indexList"
        />
        <el-table-column
          label="楼宇名称"
          width="180"
          prop="name"
        />
        <el-table-column
          label="层数"
          prop="floors"
        />
        <el-table-column
          label="在管面积(m²)"
          width="120"
          prop="area"
        />
        <el-table-column
          label="物业费(元/m²)"
          width="120"
          prop="propertyFeePrice"
        />
        <el-table-column label="状态" prop="status">
          <template #default="scope">
            {{ formatStatus(scope.row.status) }}
          </template>
        </el-table-column>

        <el-table-column
          label="操作"
          width="120"
        >
          <template #default="scope">
            <el-button
              size="mini"
              type="text"
              @click="changeADD(scope.row)"
            >编辑</el-button>
            <el-button
              size="mini"
              type="text"
              @click="deleteList(scope.row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <el-pagination
        layout="total, prev, pager, next"
        :total="total"
        @current-change="changeCurrnet"
      />
    </div>

  </div>
</template>

<script>
import { createApi, creatAddApi, deleteAPI, changeAPI } from '@/api/building'
export default {
  name: 'Building',
  data() {
    return {
      params: {
        name: '',
        page: 1,
        pageSize: 10
      },
      addFrom: {
        floors: null,
        area: null,
        propertyFeePrice: null,
        name: ''

      },
      list: [],
      total: 0,
      dialogVisible: false,
      addFromList: {
        name: [
          { required: true, message: '请输入楼宇名称', trigger: 'blur' }
        ],
        floors: [
          { required: true, message: '请输入楼宇层数', trigger: 'blur' }
        ],
        area: [
          { required: true, message: '请输入在管面积', trigger: 'blur' }
        ],
        propertyFeePrice: [
          { required: true, message: '请输入楼宇物业费', trigger: 'blur' }
        ]

      }
    }
  },

  created() {
    // 渲染页面
    this.createa()
  },
  methods: {
    // 编辑楼宇
    changeADD(row) {
      const { id, floors, area, propertyFeePrice, name } = row
      console.log(row)
      this.addFrom = { id, floors, area, propertyFeePrice, name }
      this.dialogVisible = true
    },

    // 删除楼宇
    deleteList(id) {
      this.$confirm('您确定要删除该楼宇吗?', '温馨提示').then(async() => {
        await deleteAPI(id)
        this.$message.success('删除成功')
        this.createa()
      }).catch(() => {

      })
    },
    // 添加并统一校验表单
    confirmAdd() {
      this.$refs.FromBiaoDan.validate(async(valid) => {
        if (!valid) return
        // console.log('添加')
        if (valid) {
          if (this.addFrom.id) {
            await changeAPI(this.addFrom)
            this.$message.success('编辑成功')
            this.createa()
            this.dialogVisible = false
          } else {
            await creatAddApi(this.addFrom)
            this.createa()
            this.$message.success('添加成功')
            this.dialogVisible = false
          }
        }
      })
    },
    // 弹框交换
    addBuilding() {
      this.dialogVisible = true
    },
    closeDialog() {
      this.dialogVisible = false
      this.addFrom = {}
    },
    // 当搜索框为空失去焦点时，表单自动刷新且重置表单页数为第一页
    ChongZhi() {
      if (this.params.name === '') {
        this.createa()
      }
    },
    // 查询列表
    NameCha() {
      // 将列表重置为1
      this.params.page = 1
      // 重新渲染列表
      this.createa()
    },
    // 获取楼宇列表
    async createa() {
      const res = await createApi(this.params)
      // console.log(res)
      this.list = res.data.rows
      this.total = res.data.total
    },
    // 状态渲染
    formatStatus(status) {
      const statusMap = {
        0: '租赁中',
        1: '闲置中'
      }
      return statusMap[status]
    },
    // 序号渲染
    indexList(index) {
      return (this.params.page - 1) * this.params.pageSize + index + 1
    },
    // 切换页数渲染列表
    changeCurrnet(val) {
      this.params.page = val
      this.createa()
    }
  }
}

</script>

<style lang="scss" scoped>
.department-container {
  padding: 10px;
}
.page-container{
padding:4px 0px;
text-align: right;
}
.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, 0.9);
  padding-bottom: 20px;
  .search-label {
    width: 100px;
  }
  .search-main {
    width: 220px;
    margin-right: 10px;
  }

}
</style>
